package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.*;
import com.foreknow.service.DepartmentService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.PagingTools;
//import com.foreknow.utils.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 科室管理
 */
@RestController
@RequestMapping("/departmentController")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    //单独删除
    @RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
    public String updateByPrimaryKey(@RequestBody Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int i = departmentService.updateByPrimaryKey(id);
        if (id != null&&id >=0&&i!=1) {
            hs.put("data", "添加成功");
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    //批量删除
    @RequestMapping(value = "/updateByPrimaryKeyList", method = RequestMethod.POST)
    public String updateByPrimaryKeyList(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        List<Integer> id = (List<Integer>)para.get("id");
        HashMap<String, Object> hs = new HashMap<>();
        List<String> list = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        if (id.size() != 0) {
            for(int i : id){
                int l = departmentService.updateByPrimaryKey(i);
                if (l!=1){
                    DepartmentModel departmentModel = departmentService.selectByPrimaryKey(i);
                    list.add(departmentModel.getDeptname());
                }
            }
            if (list.size()==0){
                hs.put("data", "删除成功");
            }else {
                hs.put("data", "有删除失败部分");
                hs.put("failure",list);
            }
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("data", "删除数据为空");
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //单独添加
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        Department record = new Department();
        record.setDeptcode((String)para.get("deptcode"));
        record.setDeptname((String)para.get("deptname"));
        record.setDeptcategoryid((Integer) para.get("deptcategoryid"));
        record.setDepttype((Integer)para.get("depttype"));
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        int i = departmentService.insert(record);
        if (record != null&&i==1) {
            hs.put("data", "添加成功");
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //根据id查询（单）
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
    public String selectByPrimaryKey(@RequestBody Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        DepartmentModel departmentModel = departmentService.selectByPrimaryKey(id);
        if (departmentModel!=null) {
            hs.put("data", departmentModel);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }

        return objectMapper.writeValueAsString(hs);
    }
    //查询全部信息
    @RequestMapping(value = "/SelectDepartmentAll", method = RequestMethod.POST)
    public String SelectDepartmentAll(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        Integer pageNum = (Integer) para.get("pageNum");
        Integer pageSize = (Integer) para.get("pageSize");
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        //调用查询方法
        List<DepartmentModel> departmentModel = departmentService.SelectDepartmentAll(pageNum,pageSize);
        //设置DepartmentModel内属性
        for(DepartmentModel d:departmentModel){
            d.newDepttypemodle();
            d.setDeptcategoryidname(d.getDeptcategoryid()+"-"+d.getDeptcategoryidname());
        }
        //调用读取分页方法信息并封装
        PagingTools<DepartmentModel> pagingTools = PagingTools.restPage(departmentModel);
        if (departmentModel!=null) {
            hs.put("data", pagingTools);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //模糊查询
    @RequestMapping(value = "/departmentFuzzyByName", method = RequestMethod.POST)
    public String departmentFuzzyByName(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        String name = (String) para.get("name");
        String condition = (String) para.get("condition");
        Integer pageNum = (Integer) para.get("pageNum");
        Integer pageSize = (Integer) para.get("pageSize");
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        List<DepartmentModel> departmentModel = departmentService.departmentFuzzyByName(pageNum,pageSize,condition,name);
        //设置DepartmentModel内属性
        for(DepartmentModel d:departmentModel){
            d.newDepttypemodle();
            d.setDeptcategoryidname(d.getDeptcategoryid()+"-"+d.getDeptcategoryidname());
        }
        //调用读取分页方法信息并封装
        PagingTools<DepartmentModel> pagingTools = PagingTools.restPage(departmentModel);
        if (departmentModel!=null) {
            hs.put("data", pagingTools);
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //修改信息
    @RequestMapping(value = "/updateByPrimaryKeySelective", method = RequestMethod.POST)
    public String updateByPrimaryKeySelective(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        Department record = new Department();
        record.setId((Integer) para.get("id"));
        record.setDeptcode((String)para.get("deptcode"));
        record.setDeptname((String)para.get("deptname"));
        record.setDeptcategoryid((Integer) para.get("deptcategoryid"));
        record.setDepttype((Integer)para.get("depttype"));
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        int i = departmentService.updateByPrimaryKeySelective(record);
        if (i!=0){
            hs.put("data", "添加成功");
            hs.put("meta", new Constance("msg", 200));
        }else {
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //获取科室类型列表（1-5那个）
    @RequestMapping(value = "/selectDeptTypeList", method = RequestMethod.POST)
    public String selectDeptTypeList() throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        List<DeptType> list = DeptType.getDeptType();
        if (list != null){
            hs.put("data", list);
            hs.put("meta", new Constance("msg", 200));
        }else {
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //查询科室列表
    @RequestMapping(value = "/selectDeptcategoryid", method = RequestMethod.POST)
    public String selectDeptcategoryid() throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        List<Deptcategoryid> list = departmentService.SelectDeptcategoryid();
        if (list.size() != 0){
            hs.put("data", list);
            hs.put("meta", new Constance("msg", 200));
        }else {
            hs.put("data", "未查询到数据");
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }

    //查询科室列表
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    public String DPName() throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        List<Department> list = departmentService.DPlist();
        if (list != null){
            hs.put("data", list);
            hs.put("meta", new Constance("msg", 200));
        }else {
            hs.put("data", "未查询到数据");
            hs.put("meta", new Constance("msg", 404));
        }
        return objectMapper.writeValueAsString(hs);
    }
    //导出当前查询数据
//    @RequestMapping(value = "/getSelectExcel", method = RequestMethod.POST)
//    public void getSelectExcel(HttpServletRequest request, HttpServletResponse response)
//            throws IOException {
//        List<Department> list = new ArrayList<>();
//        String queryMethod = "SelectDepartmentAll";         //request.getParameter("queryMethod");
//        if (queryMethod.equals("departmentFuzzyByName")){
//            String condition = request.getParameter("condition");
//            String name = request.getParameter("name");
//            list = Workbook.convertList(departmentService.departmentFuzzyByName(condition,name));
//        }
//        if (queryMethod.equals("SelectDepartmentAll")){
//            list = Workbook.convertList(departmentService.SelectDepartmentAll());
//        }
//        Workbook.excel(request,response,list);
//        return;
//    }
}
